<template>
    <!-- 编辑弹窗 -->
    <el-dialog v-model="store.state.storemsg.addDialogVisible" title="添加信息">
        <el-form ref="ruleFormRef" :model="addDity">
            <el-form-item label="门店名称" :label-width="formLabelWidth">
                <el-input v-model="addDity.name" autocomplete="off" />
            </el-form-item>
            <el-form-item label="门店模式" :label-width="formLabelWidth">
                <el-input v-model="addDity.model" autocomplete="off" />
            </el-form-item>
            <el-form-item label="门店地址" :label-width="formLabelWidth">
                <el-input v-model="addDity.address" autocomplete="off" />
            </el-form-item>
            <el-form-item label="门店状态" :label-width="formLabelWidth">
                <el-input v-model="addDity.state" autocomplete="off" />
            </el-form-item>
            <el-form-item label="创建时间" :label-width="formLabelWidth">
                <el-input v-model="addDity.time" autocomplete="off" />
            </el-form-item>
        </el-form>
        <template #footer>
            <span class="dialog-footer">
                <el-button @click="handlecancel">取消</el-button>
                <el-button type="primary" @click="addsubmit">确定</el-button>
            </span>
        </template>
    </el-dialog>
</template>  
 
<script setup lang="ts">
import { defineEmits, reactive } from 'vue'
import { useStore } from 'vuex';
import { ElMessage, FormInstance, } from 'element-plus'
import http from '../util/api'

const store = useStore()

const formLabelWidth = '140px'

const addDity = reactive<any>({
    id: '',
    name: '',
    model: '',
    address: '',
    state:'',
    time:''
})

 
//父组件重新获取信息
const emit = defineEmits(['getList'])
//确认添加
const addsubmit = (formEl: FormInstance | undefined) => {
    let data = {
        'id':addDity.id,
        'name': addDity.name,
        'model': addDity.model,
        'address': addDity.address,
        'state': addDity.state,
        'time': addDity.time,
    }
    
    http(`/shop/`, 'POST',data).then((res: any) => {
        if (res.status === 201) {
            ElMessage({
                type: 'success', 
                message: '添加成功！',
            })
            emit('getList')
            Object.keys(addDity).map(item => {
                delete addDity[item]
            })
        } else {
            ElMessage({
                type: 'error',
                message: '添加失败！',
            })
        }
        store.commit('changeAddDialog')
    })
 
}

//取消修改
const handlecancel = () => {
    store.commit('changeAddDialog')
    ElMessage({
        type: 'info',
        message: '已取消！',
    })
}

</script>
<style scoped>
.el-button--text {
    margin-right: 15px;
}

.el-select {
    width: 300px;
}

.el-input {
    width: 300px;
}

.dialog-footer button:first-child {
    margin-right: 10px;
}
</style>